<template>
  <div class="signup-form-wrapper">
    <div class="signup-form-title">{{ $t('signup.form.title') }}</div>
    <!--<div class="signup-form-sub-title">{{ $t('login.form.title') }}</div>-->
    <div class="signup-form-error-msg">{{ errorMessage }}</div>
    <a-form
      ref="loginForm"
      :model="userInfo"
      class="signup-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <!--  用户名 -->
      <a-form-item
        field="username"
        :rules="[{ required: true, message: $t('login.form.userName.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="userInfo.username"
          :placeholder="$t('login.form.userName.placeholder')"
        >
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <!-- 手机号 -->
      <a-form-item
        field="mobile"
        :rules="[{ required: true, message: $t('login.form.phone.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="userInfo.mobile"
          :placeholder="$t('login.form.phone.placeholder')"
        >
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <!-- 邮箱 -->
      <a-form-item
        field="email"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input v-model="userInfo.email" placeholder="请输入邮箱">
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <!-- 城市 -->
      <a-form-item
        field="city"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input v-model="userInfo.city" placeholder="请输入所在城市">
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <!-- 密码 -->
      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="userInfo.password"
          :placeholder="$t('login.form.password.placeholder')"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <!-- 整几个密保的问题 -->
      <a-form-item>
        <a-input
          v-model="userInfo.questions[0].stem"
          placeholder="请输入密保问题1"
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-input
          v-model="userInfo.questions[0].answer"
          placeholder="请输入密保问题1的答案"
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-input
          v-model="userInfo.questions[1].stem"
          placeholder="请输入密保问题2"
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-input
          v-model="userInfo.questions[1].answer"
          placeholder="请输入密保问题2的答案"
        ></a-input>
      </a-form-item>
      <a-form-item v-model="userInfo.role">
        <a-radio-group
          v-model="userInfo.role"
          style="display: flex; align-items: center; justify-content: center"
        >
          <!--<a-radio value="0">管理员</a-radio>-->
          <a-radio value="1">租户</a-radio>
          <a-radio value="2">用户</a-radio>
          <a-radio value="3">答者</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <a-button type="primary" html-type="submit" long :loading="loading">
          {{ $t('login.form.register') }}
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useI18n } from 'vue-i18n';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import type { LoginData } from '@/api/user';

  const router = useRouter();
  const { t } = useI18n();
  const errorMessage = ref('');
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();

  const loginConfig = useStorage('login-config', {
    rememberPassword: true,
    username: 'admin', // 演示默认值
    password: 'admin', // demo default value
  });
  const userInfo = reactive({
    username: loginConfig.value.username,
    password: loginConfig.value.password,
    mobile: '',
    passwordConfirm: '',
    role: '',
    email: '',
    city: '',
    questions: [
      {
        stem: '',
        answer: '',
      },
      {
        stem: '',
        answer: '',
      },
    ],
  });

  const handleSubmit = async ({
    errors,
    values,
  }: {
    errors: Record<string, ValidatedError> | undefined;
    values: Record<string, any>;
  }) => {
    console.log(userInfo);
    if (loading.value) return;
    if (!errors) {
      setLoading(true);
      try {
        console.log('values');
        console.log(values);
        console.log('values');

        await userStore.register(values as LoginData);
        await userStore.registerquestions(values.username, values.questions);
        // const { redirect, ...othersQuery } = router.currentRoute.value.query;
        // router.push({
        //   name: (redirect as string) || 'Workplace',
        //   query: {
        //     ...othersQuery,
        //   },
        // });
        Message.success(t('login.form.login.register'));
        // const { rememberPassword } = loginConfig.value;
        // const { username, password } = values;
        // // 实际生产环境需要进行加密存储。
        // // The actual production environment requires encrypted storage.
        // loginConfig.value.username = rememberPassword ? username : '';
        // loginConfig.value.password = rememberPassword ? password : '';
      } catch (err) {
        errorMessage.value = (err as Error).message;
      } finally {
        setLoading(false);
      }
    }
  };
</script>

<style lang="less" scoped>
  .signup-form {
    &-wrapper {
      width: 320px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: 500;
      font-size: 24px;
      line-height: 32px;
    }

    &-sub-title {
      color: var(--color-text-3);
      font-size: 16px;
      line-height: 24px;
    }

    &-error-msg {
      height: 32px;
      color: rgb(var(--red-6));
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }
</style>
